package com.live.dengdengtv.widget;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.aigestudio.wheelpicker.widgets.WheelDatePicker;
import com.live.dengdengtv.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by wyf on 2016/8/11.
 */
public class WheelDatePickerDialog {


    private Dialog pickDialog;

    private OnWheelDateDismiss onWheelDateDismiss;
    private WheelDatePicker main_wheel_wp;
    private TextView xingzuo_tv;
    private TextView age_tv;

    public void showDatePickerDialog(Activity activity, OnWheelDateDismiss onWheelDateDismiss) {

        this.onWheelDateDismiss = onWheelDateDismiss;

        pickDialog = new Dialog(activity, R.style.dialog_float);
        pickDialog.setContentView(R.layout.dialog_datepicker);

        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window dlgwin = pickDialog.getWindow();
        WindowManager.LayoutParams lp = dlgwin.getAttributes();
        dlgwin.setGravity(Gravity.BOTTOM);
        lp.width = (int)(display.getWidth()); //设置宽度

        pickDialog.getWindow().setAttributes(lp);

        main_wheel_wp = (WheelDatePicker) pickDialog.findViewById(R.id.main_wheel_wp);
        xingzuo_tv = (TextView) pickDialog.findViewById(R.id.xingzuo_tv);
        age_tv = (TextView) pickDialog.findViewById(R.id.age_tv);

        main_wheel_wp.setYearFrame(1949, Calendar.getInstance().get(Calendar.YEAR)-1);
        main_wheel_wp.setYear(1991);
        main_wheel_wp.setMonth(1);
        main_wheel_wp.setSelectedDay(1);
        main_wheel_wp.setOnDateSelectedListener(new WheelDatePicker.OnDateSelectedListener() {
            @Override
            public void onDateSelected(WheelDatePicker picker, Date date) {
                int year = main_wheel_wp.getCurrentYear();
                int month = main_wheel_wp.getCurrentMonth();
                int day = main_wheel_wp.getCurrentDay();
                String xingZuo = getXingZuo(month,day);
                xingzuo_tv.setText(xingZuo);
                int age = getAge(year,month,day);
                age_tv.setText(age+"岁");
            }
        });


        pickDialog.setOnDismissListener(onDismissListener);
        pickDialog.show();

    }

    private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            onWheelDateDismiss.onWheelDateDismiss(age_tv.getText().toString().trim(), xingzuo_tv.getText().toString().trim(), main_wheel_wp.getCurrentDateText());
            onWheelDateDismiss = null;
        }
    };


    public interface OnWheelDateDismiss {
        public void onWheelDateDismiss(String age, String xingZuo, String date);
    }

    public static int getAge(int year, int month, int day){
        Calendar calendar = Calendar.getInstance();
        int age = calendar.get(Calendar.YEAR)- year;
        ;
        if(calendar.getInstance().get(Calendar.MONTH) + 1 - month < 0){
            age--;
        }else if(calendar.getInstance().get(Calendar.MONTH) + 1 - month == 0){
            if(calendar.get(Calendar.DAY_OF_MONTH) - day < 0){
                age--;
            }
        }
        return age;
    }
    public static String getXingZuo(int month, int day) {

        String[] xingZuos = new String[]{"魔羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座"};

        int index = month-1;
        switch (month) {
            case 2:
                if (day > 19) index++;
                break;

            case 1:
            case 3:
            case 4:
                if (day > 20) index++;
                break;

            case 5:
            case 6:
            case 12:
                if (day > 21) index++;
                break;

            case 7:
            case 8:
            case 9:
            case 11:
                if (day > 22) index++;
                break;

            case 10:
                if (day > 23) index++;
                break;
            default: throw  new RuntimeException("请输入正确的时间!");
        }

        return xingZuos[index];
    }
}
